package ferp.center.server.servlet.config;

import ferp.center.network.request.RequestConfigBidNNUpdate;
import ferp.center.server.Action;
import ferp.center.server.entity.Config;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * User: igorgok
 * Date: 12/9/12 5:12 PM
 */
public class ConfigBidNNUpdate extends HttpServlet
{
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    Action.execute(request, response, RequestConfigBidNNUpdate.class, new Action.Write<RequestConfigBidNNUpdate, Boolean>()
    {
      @SuppressWarnings("unchecked")
      public Boolean run(EntityManager em, RequestConfigBidNNUpdate input) throws IOException
      {
        Config c = ConfigGet.load(em);

        if (c != null)
        {
          log("setting new NN data of size " + input.data.length() + ", setup: " + input.setup);

          c.setBidNNID(c.getBidNNID() + 1);
          c.setTrainingSetSize(input.tss);
          c.setBidNNSetup(input.setup);
          c.setBidNNData(input.data);
          c.setBidNNTime(input.time);

          // clear mem-cache
          ConfigGet.uncache();

          log("done.");

          return true;
        }

        return false;
      }
    });
  }
}
